import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

const Login = () => import('@/views/login')
const Layout = () => import('@/views/layout')
const Article = () => import('@/views/article')
const Dashboard = () => import('@/views/dashboard')
Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  {
    path: '/',
    redirect: '/login',
    component: Layout,
    children: [
      { path: 'article', component: Article },
      { path: 'dashboard', component: Dashboard }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && store.state.user.token === '') {
    next('/login')
    return
  }
  next()
})

export default router
